package com.itcast.donkey.adapters;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.ScenicAttackModles;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by lanouhn on 16/8/30.
 */
public class InAttackShowAdapter extends RecyclerView.Adapter<InAttackShowAdapter.ShowViewHolder> {

    private Context context;
    private List<ScenicAttackModles.PagesBean.ChildrenBean.SectionsBean> sectionsBeen;

    public InAttackShowAdapter(Context context, List<ScenicAttackModles.PagesBean.ChildrenBean.SectionsBean> sectionsBeen) {
        this.context = context;
        this.sectionsBeen = sectionsBeen;
    }

    @Override
    public ShowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_show_in_attack, null);
        ShowViewHolder holder = new ShowViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ShowViewHolder holder, final int position) {
        holder.itemShowInAttackTitle.setText(sectionsBeen.get(position).getTitle());
        holder.tvShowInAttackDescription.setText(sectionsBeen.get(position).getDescription());
        if (sectionsBeen.get(position).getUser() != null) {
            holder.tvItemShowInAttackUsername.setText(sectionsBeen.get(position).getUser().getName());
            holder.tvItemShowInAttackDate.setText(sectionsBeen.get(position).getTravel_date());
            holder.tvItemShowInAttackUsername.setVisibility(View.VISIBLE);
            holder.tvItemShowInAttackDate.setVisibility(View.VISIBLE);
        }

        if (!sectionsBeen.get(position).getPhotos().isEmpty()) {
            holder.scrollviewItemShowImage.setVisibility(View.VISIBLE);
            for (int i = 0; i < sectionsBeen.get(position).getPhotos().size(); i++) {

                ImageView image = new ImageView(context);
                ImageOptions imageOptions = new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))//图片大小
                        .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                        .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                        .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                        .setUseMemCache(true)//缓存
                        .setIgnoreGif(false)//不忽略动态图
                        .build();
                x.image().bind(image, sectionsBeen.get(position).getPhotos().get(i).getImage_url(), imageOptions);
                holder.linearLayout.addView(image);
            }
        }
        /**
         * 实体类是自己添加的，报空指针；
         */
        if (!sectionsBeen.get(position).getPages().isEmpty()) {
            holder.linItemShowInIntent.setVisibility(View.VISIBLE);
            holder.linItemShowInIntent.setBackgroundColor(Color.WHITE);
            for (int i = 0; i < sectionsBeen.get(position).getPages().size(); i++) {
                holder.tvItemShowPagesTitle.setText(sectionsBeen.get(position).getPages().get(i).getTitle());
            }

            holder.linItemShowInIntent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "id="+sectionsBeen.get(position).getPages().get(0).getId(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return sectionsBeen.size();
    }

    public class ShowViewHolder extends RecyclerView.ViewHolder {

        private TextView itemShowInAttackTitle;
        private TextView tvShowInAttackDescription;
        private HorizontalScrollView scrollviewItemShowImage;
        private TextView tvItemShowInAttackUsername;
        private TextView tvItemShowInAttackDate;
        private LinearLayout linearLayout;

        private LinearLayout linItemShowInIntent;
        private TextView tvItemShowPagesTitle;

        public ShowViewHolder(View itemView) {
            super(itemView);
            itemShowInAttackTitle = (TextView) itemView.findViewById(R.id.item_show_in_attack_title);
            tvShowInAttackDescription = (TextView) itemView.findViewById(R.id.tv_show_in_attack_description);
            scrollviewItemShowImage = (HorizontalScrollView) itemView.findViewById(R.id.scrollview_item_show_image);

            tvItemShowInAttackUsername = (TextView) itemView.findViewById(R.id.tv_item_show_in_attack_username);
            tvItemShowInAttackDate = (TextView) itemView.findViewById(R.id.tv_item_show_in_attack_date);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.lin_item_show_in_attack);

            linItemShowInIntent = (LinearLayout) itemView.findViewById(R.id.lin_item_show_in_intent);
            tvItemShowPagesTitle = (TextView) itemView.findViewById(R.id.tv_item_show_pages_title);
        }
    }
}
